package com.lby.graduation.config;

import com.lby.graduation.Interceptor.JwtInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    JwtInterceptor jwtInterceptor;
    /* 配置资源映射
    * 浏览器出于安全不允许直接通过本地路径访问，比如：D://images/aa.jpg,会报Not allowed to load local resource错误
    * 因此需要映射成服务器路径http://localhost:8081/upload/aa.jpg
    * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //上传的图片在D:/Project/upload/目录下，访问路径如：http://localhost:8081/upload/aa.jpg
        //其中upload表示访问的前缀,代替了D:/Project/upload/，因此"file:D:/upload/"是文件真实的存储路径
        registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/Project/upload/");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")//表示拦截所有请求
                .excludePathPatterns("/doLogin/**")//放行访问登录页的请求
                .excludePathPatterns("/upload/**")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("user/adminFindUser");

    }
}
